package com.example.room.service.impl;


import cn.hutool.core.bean.BeanUtil;

import com.example.core.exception.HotelException;
import com.example.model.room.Room;
import com.example.model.room_type.RoomType;
import com.example.room.feign.CheckInFeignClient;
import com.example.room.feign.RoomTypeFeignClient;
import com.example.room.mapper.RoomMapper;
import com.example.room.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.example.model.constants.CodeConstants.INSERT_ERROR;


@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RoomTypeFeignClient roomTypeFeignClient;

    @Autowired
    private CheckInFeignClient checkInFeignClient;

    @Override
    public Room get(Long id) {
        Room room = roomMapper.get(id);
        room.setRoomType(BeanUtil.toBean(roomTypeFeignClient.get(room.getRoomTypeId()).getData(),RoomType.class));
        return room;
    }

    @Override
    public Room getByNo(String roomNo) {
        Room room = roomMapper.getByRoomNo(roomNo);
        room.setRoomType(BeanUtil.toBean(roomTypeFeignClient.get(room.getRoomTypeId()).getData(),RoomType.class));
        return room;
    }

    @Override
    public List<Room> list(Room room) {
        return roomMapper.list(room).stream()
                .peek(room1 ->
                        room1.setRoomType(
                                BeanUtil.toBean(roomTypeFeignClient.get(room1.getRoomTypeId()).getData(),
                                RoomType.class)))
                .collect(Collectors.toList());
    }

    @Override
    public int insert(Room room) {
        int count = roomMapper.listCountByRoomNo(room.getRoomNo());
//        Room existingRoom = roomMapper.getByRoomNo(room.getRoomNo());
       if (count == 1) {
            throw new HotelException(INSERT_ERROR,"房间号重复");
        }
        return roomMapper.insert(room);
    }

    @Override
    public int update(Room room) {
        return roomMapper.update(room);
    }

    @Override
    public int delete(Long id) {
        return roomMapper.delete(id);
    }

    @Override
    public List<Room> enableCheckIn(String checkInDate) {
        List<Room> allRoomList = roomMapper.list(null);
        List<Room> statusZeroList = allRoomList.stream().filter(room -> room.getStatus() == 0).toList();
        List<Room> statusOneEnableList = allRoomList.stream()
                .filter(room -> room.getStatus() == 1)
                .filter(room ->
                        checkInFeignClient.isEnable(room.getRoomId(), checkInDate).getData().toString().equals("true"))
                .toList();
        return Stream.concat(statusZeroList.stream(), statusOneEnableList.stream())
                .peek(room ->
                        room.setRoomType(BeanUtil.toBean(roomTypeFeignClient.get(room.getRoomTypeId()).getData(),
                                RoomType.class)))
                .toList();
    }


}
